package com.derschmale.wick3d.core.io.collada
{
	import com.derschmale.wick3d.core.geometry.Triangle3D;
	import com.derschmale.wick3d.core.geometry.Vertex3D;
	import com.derschmale.wick3d.core.io.collada.data.ColladaGeometry;
	import com.derschmale.wick3d.core.objects.Model3D;
	import com.derschmale.wick3d.materials.WireframeMaterial;
	
	public class DAEModel extends Model3D
	{
		private var _name : String;
		private var _id : String;
		
		public function DAEModel(geometry : ColladaGeometry, id : String, name : String)
		{
			super(new WireframeMaterial(0xff00ff));
			translate(geometry);
			_name = name;
			_id = id;
			trace (_name, _id);
		}
		
		public function get name() : String
		{
			return _name;
		}
		
		public function get id() : String
		{
			return _id;
		}
		
		private function translate(geometry : ColladaGeometry) : void
		{
			var triangle : Triangle3D;
			var index1 : int, index2 : int, index3 : int;
			
			cloneVertices(geometry.vertices);
			
			for (var i : int = 0; i < geometry.vertexFaceIndices.length; i+=3) {
				index1 = geometry.vertexFaceIndices[i+2];
				index2 = geometry.vertexFaceIndices[i+1];
				index3 = geometry.vertexFaceIndices[i];
				triangle = new Triangle3D(_trVertices[index1], _trVertices[index2], _trVertices[index3]);
				triangle.setParent(this, _vertices[index1], _vertices[index2], _vertices[index3]);
				
				index1 = geometry.uvFaceIndices[i+2];
				index2 = geometry.uvFaceIndices[i+1];
				index3 = geometry.uvFaceIndices[i];
				triangle.uv1 = geometry.uvcoords[index1];
				triangle.uv2 = geometry.uvcoords[index2];
				triangle.uv3 = geometry.uvcoords[index3];
				triangle.material = material;
				_triangles.push(triangle);
			}
		}
		
		private function cloneVertices(vertices : Array) : void
		{
			for (var i : int = 0; i < vertices.length; i++) {
				_vertices.push(Vertex3D(vertices[i]).clone());
				_trVertices.push(Vertex3D(vertices[i]).clone());
			}
		}
		
	}
}